/**
 *
 */
package net.zantetu.matomeroid.manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;

/**
 * @author aono_masashi
 *
 */
public class FileManager {

	// 隠蔽
	private FileManager(){}

	/**
	 * パスからファイルを読み込んでString化
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readFile(Context context, String path) throws IOException{
		StringBuilder sb = new StringBuilder();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(context.getAssets().open(path),"UTF-8"));
			String line = null;
			while((line = br.readLine()) != null){
				sb.append(line).append(System.getProperty("line.separator"));
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if(br != null){
				try {
					br.close();
				} catch (Exception e) {}
			}
		}

		return sb.toString();
	}

	/**
	 * pageIdのページの内容を取得
	 * @param pageId
	 * @return
	 * @throws IOException
	 */
	public static String readPage(Context context, int pageId) throws IOException{
		return readFile(context, makePageFilePath(pageId));
	}

	/**
	 * pageIdからpageのfilepathを生成
	 * @param pageId
	 * @return
	 */
	private static String makePageFilePath(int pageId){
		return "pages/" + pageId;
	}
}
